﻿using System;
using System.Text;

namespace Solution
{
    public class ExceptionExtension
    {
        #region 公共属性

        /// <summary>
        ///     异常输出的详细描述，包含异常消息，规模信息，异常类型，异常源，引发异常的方法及内部异常信息
        /// </summary>
        public static string ErrorDetails
        {
            get; private set;
        }

        /// <summary>
        ///     直接的Exception异常信息，即e.Message属性值
        /// </summary>
        public static string ExMessage
        {
            get; private set;
        }

        /// <summary>
        ///     用户信息，用于报告给用户的异常消息
        /// </summary>
        public static string UserMessage
        {
            get; set;
        }

        #endregion 公共属性

        #region 公共方法

        /// <summary>
        ///     以自定义用户信息和异常对象实例化一个异常信息对象
        /// </summary>
        /// <param name="e"> 异常对象 </param>
        /// <param name="userMessage"> 自定义用户信息 </param>
        /// <param name="isHideStackTrace"> 是否隐藏异常堆栈信息 </param>
        /// <param name="log"> 是否自动写入错误日志 </param>
        public static void ExceptionMessageInit(Exception e, string userMessage = null, bool isHideStackTrace = false, bool log = true)
        {
            UserMessage = string.IsNullOrEmpty(userMessage) ? e.Message : userMessage;

            StringBuilder sb = new StringBuilder();
            ExMessage = string.Empty;
            int count = 0;
            string appString = "";
            sb.AppendLine(appString + "自定义消息：" + userMessage);
            while (e != null)
            {
                if (count > 0)
                {
                    appString += "　";
                }
                ExMessage = e.Message;
                sb.AppendLine(appString + "异常消息：" + e.Message);
                sb.AppendLine(appString + "异常类型：" + e.GetType().FullName);
                sb.AppendLine(appString + "异常方法：" + (e.TargetSite == null ? null : e.TargetSite.Name));
                sb.AppendLine(appString + "异常源：" + e.Source);
                if (!isHideStackTrace && e.StackTrace != null)
                {
                    sb.AppendLine(appString + "异常堆栈：" + e.StackTrace);
                }
                if (e.InnerException != null)
                {
                    sb.AppendLine(appString + "内部异常：");
                    count++;
                }
                e = e.InnerException;
            }
            ErrorDetails = sb.ToString();
            sb.Clear();
            //自动写错误日志
            if (log)
            {
                Logger.Error("system", e);
            }
        }

        /// <summary>
        /// 获得完整的异常信息
        /// </summary>
        /// <param name="exception"></param>
        /// <returns></returns>
        public static string GetAllMessages(Exception exception)
        {
            var ex = exception;
            var sb = new StringBuilder();
            while (ex != null)
            {
                sb.AppendLine(ex.Message);
                ex = ex.InnerException;
            }
            return sb.ToString();
        }

        #endregion 公共方法
    }
}